package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.domain.ClassPlanDetail;
import com.jsyl.lwbk.business.dto.ClassPlanDetailDTO;
import com.jsyl.lwbk.business.dto.SaveProjectDTO;
import com.jsyl.lwbk.business.service.ClassPlanDetailService;
import com.jsyl.lwbk.business.service.TrainPackageService;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.domain.Ztree;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 训练计划详情
 */
@Controller
@RequestMapping("/business/classPlanDetail")
public class ClassPlanDetailController extends BaseController {

    private String prefix = "business/classPlan";

    @Autowired
    ClassPlanDetailService classPlanDetailService;
    @Autowired
    private TrainPackageService trainPackageService;

    /**
     * 页面跳转-计划详情主页
     *
     * @param classPlanId
     * @param totalClassTimes
     * @param mmap
     * @return
     */
    @GetMapping("/list/{classPlanId}/{totalClassTimes}")
    public String list(@PathVariable("classPlanId") String classPlanId,
                       @PathVariable("totalClassTimes") String totalClassTimes, ModelMap mmap) {
        mmap.put("classPlanId", classPlanId);
        mmap.put("totalClassTimes", totalClassTimes);
        return prefix + "/classPlanDetailList";
    }

    /**
     * 页面跳转-计划选择项目
     *
     * @param classPlanDetailId
     * @param linkRuleType
     * @param mmap
     * @return
     */
    @GetMapping("/checkProject/{classPlanDetailId}/{linkRuleType}")
    public String checkProject(@PathVariable("classPlanDetailId") String classPlanDetailId,
                               @PathVariable("linkRuleType") Integer linkRuleType, ModelMap mmap) {
        mmap.put("classPlanDetailId", classPlanDetailId);
        mmap.put("linkRuleType", linkRuleType);
        return prefix + "/checkProject";
    }

    /**
     * 获取选择的项目树
     *
     * @param classPlanDetailId
     * @param linkRuleType
     * @return
     */
    @GetMapping("/getProjectTree")
    @ResponseBody
    public List<Ztree> getProjectTree(@RequestParam Long classPlanDetailId, @RequestParam Integer linkRuleType) {
        List<Ztree> list = new ArrayList<>();
        //自选环节 返回选择项目
        if (linkRuleType == 1 || linkRuleType == 3) {
            list = classPlanDetailService.getProjectTree(classPlanDetailId);
        }
        //训练包环节 返回训练包
        if (linkRuleType == 2) {
            list = trainPackageService.trainPackageTree(classPlanDetailId);
        }
        return list;
    }

    /**
     * 保存选择得项目、训练包
     *
     * @param dto
     * @return
     */
    @PostMapping("/saveProject")
    @ResponseBody
    public AjaxResult saveProject(@RequestBody SaveProjectDTO dto) {
        dto.setUpdateUser(ShiroUtils.getLoginName());
        this.classPlanDetailService.saveProject(dto);
        return success();
    }

    /**
     * 保存调整项目顺序
     *
     * @param dto
     * @return
     */
    @PostMapping("/sortProject")
    @ResponseBody
    public AjaxResult sortProject(@RequestBody SaveProjectDTO dto) {
        dto.setUpdateUser(ShiroUtils.getLoginName());
        this.classPlanDetailService.sortProject(dto);
        return success();
    }

    /**
     * 根据id、课次获取计划详情
     *
     * @param classPlanId
     * @param classTimes
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findList(@RequestParam Long classPlanId, @RequestParam Integer classTimes) {
        startPage();
        List<ClassPlanDetail> list = classPlanDetailService.findList(classPlanId, classTimes);
        return getDataTable(list);
    }

    /**
     * 修改环节规则
     *
     * @param id
     * @param linkRuleType
     * @return
     */
    @PostMapping("/updateLinkRuleType")
    @ResponseBody
    public AjaxResult updateLinkRuleType(@RequestParam String id, @RequestParam Integer linkRuleType) {
        classPlanDetailService.updateLinkRuleType(Long.valueOf(id), linkRuleType);
        return success();
    }

    @PostMapping("/listThemeProject")
    @ResponseBody
    public TableDataInfo listTheme(@RequestParam Long classPlanId) {
        startPage();
        List<ClassPlanDetailDTO> list =classPlanDetailService.listThemeProject(classPlanId);
        return getDataTable(list);
    }


}
